package com.review.group.service.impl;

import java.util.List;

import com.review.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.review.group.mapper.ReviewGroupMapper;
import com.review.group.domain.ReviewGroup;
import com.review.group.service.IReviewGroupService;

/**
 * 小组Service业务层处理
 *
 * @author lls
 * @date 2022-03-16
 */
@Service
public class ReviewGroupServiceImpl implements IReviewGroupService {
    @Autowired
    private ReviewGroupMapper reviewGroupMapper;

    /**
     * 查询小组
     *
     * @param id 小组主键
     * @return 小组
     */
    @Override
    public ReviewGroup selectReviewGroupById(Long id) {
        return reviewGroupMapper.selectReviewGroupById(id);
    }

    /**
     * 查询小组列表
     *
     * @param reviewGroup 小组
     * @return 小组
     */
    @Override
    public List<ReviewGroup> selectReviewGroupList(ReviewGroup reviewGroup) {
        return reviewGroupMapper.selectReviewGroupList(reviewGroup);
    }

    /**
     * 新增小组
     *
     * @param reviewGroup 小组
     * @return 结果
     */
    @Override
    public int insertReviewGroup(ReviewGroup reviewGroup) {
        reviewGroup.setCreateTime(DateUtils.getNowDate());
        reviewGroupMapper.insertReviewGroup(reviewGroup);
        Long[] ids = reviewGroup.getUserIds();
        Long id = reviewGroup.getId();
        System.out.println(ids);
//        reviewGroupMapper.insertReviewGroupUser(ids, id);
        return 1;
    }

    /**
     * 修改小组
     *
     * @param reviewGroup 小组
     * @return 结果
     */
    @Override
    public int updateReviewGroup(ReviewGroup reviewGroup) {
        return reviewGroupMapper.updateReviewGroup(reviewGroup);
    }

    /**
     * 批量删除小组
     *
     * @param ids 需要删除的小组主键
     * @return 结果
     */
    @Override
    public int deleteReviewGroupByIds(Long[] ids) {
        reviewGroupMapper.deleteReviewGroupByIds1(ids);
        reviewGroupMapper.deleteReviewGroupByIds2(ids);
        reviewGroupMapper.deleteReviewGroupByIds3(ids);
        reviewGroupMapper.deleteReviewGroupByIds4(ids);
        return reviewGroupMapper.deleteReviewGroupByIds(ids);
    }

    /**
     * 删除小组信息
     *
     * @param id 小组主键
     * @return 结果
     */
    @Override
    public int deleteReviewGroupById(Long id) {
        return reviewGroupMapper.deleteReviewGroupById(id);
    }

    @Override
    public int addProjects(ReviewGroup reviewGroup) {
        Long[] userIds = reviewGroupMapper.selectGroupUser(reviewGroup);
        Long[] proIds = reviewGroup.getProIds();
        Long groupId = reviewGroup.getId();
        if(userIds.length!=0){
            reviewGroupMapper.addGroupUserPro(userIds, proIds, groupId);
        }

        reviewGroupMapper.addProjects2(proIds, groupId);
        return reviewGroupMapper.addProjects(proIds, groupId);
    }

    @Override
    public int addGroupUsers(ReviewGroup reviewGroup) {
        Long[]proIds=reviewGroup.getProIds();
        Long[]userIds=reviewGroup.getUserIds();
        Long groupId = reviewGroup.getId();
        if (proIds.length!=0){
            reviewGroupMapper.addGroupUsersScore(userIds,proIds,groupId);
        }
        return reviewGroupMapper.addGroupUsers(userIds,groupId);
    }

    @Override
    public Long[] selectGroupUser(ReviewGroup reviewGroup) {
        return reviewGroupMapper.selectGroupUser(reviewGroup);
    }
}
